﻿namespace Web.Control.WWW.files
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections.Generic;

    using Lx.Web.Common.ControlsUI;
    using BLF.WWW_WWW.Files;
    using Model.WWW_WWW.files;

    public class UpdateFile : BaseControl
    {
        public FileDocModel DocModel;
        public List<string> TagList;
        protected override void OnLoad(EventArgs e)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                Page.Response.Redirect(WWWHost + "/User/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri);
                return;
            }
            //更新文件
            if (Request.Form["action"] == "update")
            {
                string docId, title, learnLang, baseLang, level, desc, tags, txtTags;// memberNo;
                docId = Request.Form["docid"];
                title = Request.Form["txtTitle"];
                learnLang = Request.Form["learLang"];
                baseLang = Request.Form["baseLang"];
                level = Request.Form["level"];
                desc = Request.Form["txtDesc"];
                tags = Request.Form["tags"];
                txtTags = Request.Form["txtTags"];

                //判断参数是否正确
                if (docId.Length <= 0
                    || title.Length <= 0
                    || learnLang.Length <= 0
                    || baseLang.Length <= 0
                    )
                {
                    Response.End();
                    return;
                }

                FileDocModel updateModel = GetMemberDoc(docId, HttpContext.Current.User.Identity.Name, out TagList);
                if (updateModel == null)
                {
                    Response.End();
                    return;
                }

                updateModel.Title = title;
                updateModel.LanguageLearn = learnLang;
                updateModel.BaseLanguage = baseLang;
                updateModel.Level = level;
                updateModel.Desc = desc;

                
                string[] totalTagList;
                
                if (tags == null)
                {
                    totalTagList = txtTags.Trim().Split(' ');
                }
                else{
                    string[] tagList = tags.Trim().Split(',');
                    string[] txtTagList = txtTags.Split(' ');
                    totalTagList = new string[tagList.Length + txtTagList.Length];
                    tagList.CopyTo(totalTagList, 0);
                    txtTagList.CopyTo(totalTagList, tagList.Length);
                }

                if (Files.UpdateFile(updateModel, totalTagList) > 0)
                    Response.Redirect("/files/doc/" + docId + ".htm");
                else
                    Response.Write("Database Error");

            }
            else
            {//读取文件

                //判断参数是否正确
                if (string.IsNullOrEmpty(Request.QueryString["docid"]))
                {
                    Response.End();
                    return;
                }
                DocModel = GetMemberDoc(Request.QueryString["docid"], HttpContext.Current.User.Identity.Name, out TagList);
                if (DocModel == null)
                {
                    Response.End();
                    return;
                }


            }
            base.OnLoad(e);
        }

        private FileDocModel GetMemberDoc(string docid, string memberNo, out List<string> tags)
        {
            //取出文档模型
            List<DocCommentModel> tmp1 = new List<DocCommentModel>();
            List<FileDocModel> tmp2 = new List<FileDocModel>();
            FileDocModel model = Files.GetAFile(docid, 0, out tmp1, 0, out tmp2, out tags);
            if (model == null)
                return null;
            if (model.MemberNo != memberNo)
                return null;
            return model;

        }
    }
}